/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.tramite.converters;

import cip.util.ControladorAbstracto;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import org.cip.esymons.general.be.Rubro;
import org.cip.esymons.tramite.beans.RubroController;

/**
 *
 * @author christian
 */
public class RubroConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null) {
            return null;
        }
        try {
            Integer id = new Integer(value);
            if (id == 0) {
                return new Rubro(0, "Todos");
            }
            RubroController controller = (RubroController) ControladorAbstracto.getBean("RubroController");
            return controller.buscarRubroPorCodigo(id);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof Rubro) {
            Rubro r = (Rubro) object;
            return "" + r.getRubCodigo();
        } else {
            return null;
        }
    }
}
